$(function() {
    $.ajaxSetup({
        type: "post",
        dataType: "json"
    });

    var provid = 0;
    var topicGalleryTemplate = _.template($("#topic-gallery-template").html());

    var type2txt = {
        0: "管理员推荐",
        1: "公共最热",
        2: "区域最热",
        3: "随机"
    };

    function renderGalleryList(provid) {
        var galleryListUrl = window.location.pathname;
        var data = {
            provid: provid
        };

        $.ajax({
            url: galleryListUrl,
            data: data,
            success: function(res) {
                if(res.ret == 0) {
                    $(".gallery-list").empty();
                    $.each(res.res, function(idx, galleryInfo) {
                        $(".gallery-list").append(
                            topicGalleryTemplate($.extend({}, galleryInfo, {type2txt: type2txt}))
                        );
                    });
                } else {
                    alert(res.msg)
                }
            },
            error: function() {

            }
        });
    }

    $(".new-gallery").on("click", function(e) {
        e.preventDefault();

        $(".new-gallery-panel").toggle();
    });

    $(".add-gallery").on("click", function(e) {
        e.preventDefault();

        var galleryInfo = {};

        var gtype = parseInt($("[name='new-type']:checked").val());

        galleryInfo['gtype'] = gtype;

        if (gtype == 0) {
            var remark = $("#sugg-topic").val();
        } else {
            remark = "";
        }

        galleryInfo['remark'] = remark;

        $(".gallery-list").append(
            topicGalleryTemplate($.extend({}, galleryInfo, {type2txt: type2txt}))
        );
    });

    $("body").on("click", ".del-gallery", function(e) {
        e.preventDefault();

        var $current = $(e.currentTarget);

        $current.closest(".gallery-panel").remove();
    });

    $("#submit-g").on("click", function(e) {
        e.preventDefault();

        var gallerys = [];
        $(".gallery-panel").each(function() {
            var $this = $(this);
            var gtype = $this.find("[name=gtype]").val();
            var remark = $this.find("[name=remark]").val();
            gallerys.push({
                gtype: gtype,
                remark: remark
            });
        });

        var galleryAddUrl = "/cms/topic/gallery/add";
        var data = {
            provid: provid,
            gallery: JSON.stringify(gallerys)
        };

        $.ajax({
            url: galleryAddUrl,
            data: data,
            success: function(res) {
                if(res.ret == 0) {
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            },
            error: function() {

            }
        });
    });

    $("#refresh-g").on("click", function(e) {
        e.preventDefault();

        var url = "/cms/topic/gallery/refresh";
        var data = {
            provid: provid
        };

        $.ajax({
            url: url,
            data: data,
            success: function(res) {
                if(res.ret == 0) {
                    alert("刷新成功");
                }
            },
            error: function() {

            }
        });
    });

    renderGalleryList(provid);
});